const app = getApp();
Page({
  data: {
    xhx_images: app.xhx_images,
    xhx_route: app.xhx_route,
    prevPage: null,
  },
  onLoad,
  onReachBottom, //页面上拉触底事件的处理函数
  listDataInit,
  getPageList, //获取列表
  getUser,
  comfirm,
})

function onLoad() {
  const that = this;
  that.listDataInit();
}

function onReachBottom() {
  const that = this;
  that.getPageList(true);
}

function listDataInit() {
  const that = this;
  that.setData({
    keyword: '', //搜索框输入值
    lists: [], //列表数据
    msg: '请稍等...', //提示信息
    footMsg: "没有更多数据啦！", //底部提示信息
    page: 1, //当前页数
    row: 10, //1页的条数
    isReach: false, //是否全部加载完毕
  })
  that.getPageList();
  return true;
}

/**
 * 获取列表
 * param isReachBottom [Boolean] 是否上拉加载触发
 */
function getPageList(isReachBottom) {
  var that = this;
  var lists = that.data.lists;
  var row = that.data.row;
  var page = that.data.page;
  var keyword = that.data.keyword;
  var isReach = that.data.isReach;
  var footMsg = "";

  if (isReachBottom) {
    if (isReach) return false;
    page++;
  }

  var reqData = {
    pageSize: row,
    pageNum: page,
    keyword: keyword,
  }

  app.middleware.requestAjax({
    serAddr: app.serAddrs.focusList,
    data: reqData,
    cancelToast: true,
    success: function(res) {
      console.log(res)
      var tempLists = res.data || [];
      var lists = [];

      var prevPage = getCurrentPages()[getCurrentPages().length - 2] || {};
      if (prevPage.route) {
        var users = prevPage.data.users || [];
        if (users.length > 0) {
          for (var i = 0; i < users.length; i++) {
            for (var j = 0; j < tempLists.length; j++) {
              if (users[i].uuid == tempLists[j].uuid && users[i].check) {
                tempLists[j].check = users[i].check;
                tempLists[j].isHide = true;
              }
            }
          }
        }
      }

      if (row <= tempLists.length) {
        isReach = false;
        footMsg = "上拉加载更多";
      } else {
        isReach = true;
        footMsg = "没有更多数据啦！";
      }
      that.setData({
        msg: "",
        page: page,
        lists: lists.concat(tempLists),
        prevPage: prevPage,
        isReach: isReach,
        footMsg: footMsg,
      })
    },
    fail: function(res) {

      if (isReachBottom) {
        that.setData({
          footMsg: res && res.msg || "加载失败",
        })
      } else {
        that.setData({
          msg: res && res.msg || "加载失败",
        })
      }

    }
  })
}

function getUser(event) {
  const that = this;
  const item = event.currentTarget.dataset.item;
  const index = event.currentTarget.dataset.index;
  var lists = that.data.lists;

  lists[index].check = lists[index].check ? false : true; //当前用户被@状态
  that.setData({
    lists,
  })
  that.comfirm();
}

function comfirm() {
  const that = this;
  var prevPage = that.data.prevPage;
  var lists = that.data.lists;
  var users = lists.filter(key => key.check && !key.isHide);
  var form = prevPage.data.form;
  var prevUsers = prevPage.data.users || [];

  if(users.length>0){
    users.forEach((item)=>{
      form.releaseContent += `@${item.username} `;
      prevUsers.push(item);
    })
  }
  prevPage.setData({
    form,
    users:prevUsers,
  })
  wx.navigateBack();
}